<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Upload and Predict</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f4;
        }
        .container {
            text-align: center;
        }
        #captured-photo {
            max-width: 100%;
            max-height: 400px;
            margin-top: 20px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
        }
        button:hover {
            background-color: #45a049;
        }
        .result {
            margin-top: 20px;
            font-size: 18px;
        }
    </style>
</head>
<body>

    <div class="container">
        <h1>Capture and Predict</h1>
        <button id="capture-button">Capture Photo</button>
        <img id="captured-photo" src="" alt="Captured Photo" style="display:none;">
        <div class="result" id="prediction-result" style="display:none;">
            <h3>Prediction Result:</h3>
            <p id="prediction-label"></p>
            <p id="prediction-confidence"></p>
        </div>
    </div>

    <script>
        // 当点击按钮时捕获图片
        document.getElementById('capture-button').addEventListener('click', function() {
            const imgElement = document.getElementById('captured-photo');
            const resultElement = document.getElementById('prediction-result');
            const labelElement = document.getElementById('prediction-label');
            const confidenceElement = document.getElementById('prediction-confidence');

            imgElement.style.display = 'none';
            resultElement.style.display = 'none';  // 隐藏结果显示区域
            imgElement.src = '';  // 清空图片
            labelElement.textContent = '';
            confidenceElement.textContent = '';

            // 发起请求拍照并上传图片
            const capture_url = 'http://192.168.1.5:18888/capture_photo'
            const predict_url = 'http://192.168.1.5:18888/predict_img'
            const led_url = 'http://192.168.1.5:18888/led/'
            fetch(capture_url)
                .then(response => response.blob())
                .then(imageBlob => {
                    const imageObjectURL = URL.createObjectURL(imageBlob);
                    imgElement.src = imageObjectURL;
                    imgElement.style.display = 'block';  // 显示图片

                    // 创建 FormData 对象上传图片
                    const formData = new FormData();
                    formData.append("file", imageBlob, "captured_image.jpg");

                    // 上传图片并获取预测结果
                    fetch(predict_url, {
                        method: 'POST',
                        body: formData,
                    })
                    .then(response => response.json())
                    .then(data => {
                        // 显示分类结果
                        labelElement.textContent = `Class: ${data.class}`;
                        if (data.class === 'apple') {
                            fetch(led_url, {
                                method: 'POST',
                                headers: {
                                    'Content-Type': 'application/json'  // 设置请求头为 JSON
                                },
                                body: JSON.stringify({  // 使用 JSON.stringify 发送对象
                                    status: 1,
                                    led: "red"
                                })
                            }).then(res => res.json()).then(data => {
                                console.log(data)
                            })

                        }
                        confidenceElement.textContent = `Confidence: ${data.confidence}`;
                        resultElement.style.display = 'block';
                    })
                    .catch(error => {
                        console.error('Error predicting:', error);
                        alert('Failed to predict');
                    });
                })
                .catch(error => {
                    console.error('Error capturing photo:', error);
                    alert('Failed to capture photo');
                });
        });
    </script>

</body>
</html>
